/*
 * Copyright kecp
 * Apr 24, 2010
 */

package LBS;

import Tool.MathExtend;

/**
 * 位置，包括经纬度地理坐标以及地点名字。
 * @author kecp
 */
public class Location {
    /** 纬度 */
    private double lat;
    /** 精度 */
    private double lon;
    /** 精确度*/
    private double accuracy;
    /** 名字 */
    private String name;


    public Location() {
        lat = lon = accuracy = 0;
        name = "";
    }

    /**
     * @return the lat
     */
    public double getLat() {
        return lat;
    }

    /**
     * @param lat the lat to set
     */
    public void setLat(double lat) {
        this.lat = lat;
    }


     /**
     * @param lat the lat to set
     */
    public void setLat(String latStr) {
        if( latStr != null )
            this.lat = Double.valueOf(latStr).doubleValue();
    }

    /**
     * @return the lon
     */
    public double getLon() {
        return lon;
    }

    /**
     * @param lon the lon to set
     */
    public void setLon(double lon) {
        this.lon = lon;
    }

    /**
     * @param lon the lon to set
     */
    public void setLon(String lonStr ) {
        if( lonStr != null )
            this.lon = Double.valueOf(lonStr).doubleValue();
    }

    /**
     * @return the accuracy
     */
    public double getAccuracy() {
        return accuracy;
    }

    /**
     * @param accuracy the accuracy to set
     */
    public void setAccuracy(double accuracy) {
        this.accuracy = accuracy;
    }


     /**
     * @param accuracy the accuracy to set
     */
    public void setAccuracy(String accStr) {
         if( accStr != null )
            this.accuracy = Double.valueOf(accStr).doubleValue();

    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * caculate the distance of two location by lon and lat
     * @param  lctA location A
     * @param  lctB  location B
     * @return  the distance in meters between A and B
     */
    static public int  calDistance( Location lctA, Location lctB ) {
        double Pi = Math.PI;
        int R = 6371004;
        double C;

        C = Math.sin(lctA.lat) * Math.sin(lctB.lat) * Math.cos(lctA.lon - lctB.lon)
              + Math.cos(lctA.lat) * Math.cos(lctB.lat);

        int distance = (int) (R * MathExtend.arcCos(C) * Pi / 180);

        return distance;
    }

    public String toString(){
        StringBuffer sb = new StringBuffer();
        sb.append("accuracy:" + accuracy + ";");
        sb.append( "latitude:" + lat + ";");
        sb.append( "longitude:" + lon + ";" );
        sb.append( "name:" + name + ";");
        return sb.toString();
    }


    static public Location getTestLocation(){
        Location loc = new Location();
        loc.setLat("39.507161393258");
        loc.setLon("116.11857605617978");
        return loc;
    }
    static public void testDistance() {
        Location a = new Location();
        Location b = new Location();
        Location c = new Location();
        a.lat = 23.0438888888889; //B15
        a.lon = 113.368611111111;
        b.lat = 23.0477777777778; //北亭村
        b.lon = 113.373055555556;
        c.lat = 23.4416666666667; //从化北回归线公园
        c.lon = 113.482222222222 ;
        System.out.println( Location.calDistance(a, c) ) ;

    }
}
